package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.json.Mist;
import com.sirap.basic.json.MistUtil;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.Colls;
import com.sirap.basic.util.DateUtil;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.common.domain.Album;

public class EaseImageFetchers {
	public static void main(String[] args) {
		Object va;
//		va = getAlbums(3);/
//		va = countOfImages("http://sports.163.com/photoview/4TM10005/164442.html#p=E296JT1A4TM10005NOS");
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbumsByPage(82);
//		va = getAlbum("http://slide.sports.sina.com.cn/n_zjmaster/slide_2_789_199926.html#p=5");
//		va = getAlbum("http://news.163.com/photoview/00AO0001/2298204.html?from=ph_ss#p=E24AQNA300AP0001NOS");
		//http://sports.163.com/photoview/4TM10005/164442.html
//		va = getAlbum("http://sports.163.com/photoview/4TM10005/164442.html");
		//http://news.163.com/photoview/00AP0001/2298248.html
		va = getAlbum("http://news.163.com/photoview/3R710001/2297383.html?from=ph_kk#p=DVD5N1T63R710001NOS");
//		va = getSportsAlbumsByPage(1, 2);
//		va = getNewsAlbumsByPage(1, 2);
		D.pjsp(va);
	}

	public static Map<String, Object> getNewsAlbumsByPage(int page, int pageSize) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String temp = "http://pic.news.163.com/photocenter/api/list/0001/00AN0001,00AO0001,00AP0001/{0}/{1}/cacheMoreData.json";
	        	String url = StrUtil.occupy(temp, (page - 1) * pageSize, pageSize);
				showFetching();
				return url;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String r1 = "cacheMoreData\\((.+)\\)";
				String json = StrUtil.findFirstMatchedItem(r1, source);
				Object data = MistUtil.ofJsonText(json).getCore();

				item.put("totalpage", "1999");
				item.put("count", ((List)data).size());
				item.put("photos", data);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Map<String, Object> getKankeAlbumsByPage(int page, int pageSize) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String temp = "http://pic.news.163.com/photocenter/api/list/0001/3R710001/{0}/{1}/cacheMoreData.json";
	        	String url = StrUtil.occupy(temp, (page - 1) * pageSize, pageSize);
				showFetching();
				return url;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String r1 = "cacheMoreData\\((.+)\\)";
				String json = StrUtil.findFirstMatchedItem(r1, source);
				Object data = MistUtil.ofJsonText(json).getCore();
				
				List list = (List)data;
				Collections.shuffle(list);

				item.put("totalpage", "299");
				item.put("count", ((List)data).size());
				item.put("photos", data);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Map<String, Object> getSportsAlbumsByPage(int pageNumber, int pageSize) {
    	Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String urlsports = "http://sports.163.com/special/00050ICK/photoall0005.js";
				showFetching();
				return urlsports;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String regex = "photoList\\((.+?)\\)";
				String jsonText = StrUtil.findFirstMatchedItem(regex, source);
				Mist mist = MistUtil.ofJsonText(jsonText);
				List all = (List)mist.valueOf("photos");

				int totalpage = MathUtil.totalPageOf(all.size(), pageSize);
				item.put("totalpage", totalpage);
				
				List photos = Colls.byPage(all, pageNumber, pageSize);
				item.put("count", photos.size());
				
				item.put("photos", photos);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Extractor<Album> frank = new Extractor<Album>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return albumurl;
			}
			protected void fetchx() {
				String dir = "E:/KDB/tasks/1126_AlbumSina/fake.txt";
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {
				String regex = "<textarea[^<>]+>(.+?)</textarea";
				String data = StrUtil.findFirstMatchedItem(regex, source);
				Mist mist = MistUtil.ofJsonText(data);
				String name = mist.valueOf("info.setname") + "";
				List listObj = (List)mist.valueOf("list");
				List<String> links = Lists.newArrayList();
				for(Object obj : listObj) {
					String link = MistUtil.ofMapOrList(obj).findStringBy("img");
					links.add(link);
				}
				String when = mist.findStringBy("info.lmodify");
				Date whenDate = DateUtil.dateOf(when, DateUtil.DATE_TIME);

				item = Album.of(name, links).setUrl(albumurl).setTag("163").setWhen(when).setWhenDate(whenDate);
				item.setListObj(listObj);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static String countOfImages(String niceurl) {
		Extractor<String> frank = new Extractor<String>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return niceurl;
			}
			
			@Override
			protected void parse() {
				String regex = "\"imgsum\"\\s*:\\s*(\\d+),";
				item = StrUtil.findFirstMatchedItem(regex, source);
			}
		};
		
		return frank.process().getItem();
	}
}
